package com.igeek.boot.mapper;

import com.igeek.boot.entity.Menu;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author jwj
 * @since 2023-12-27
 */
@Mapper
public interface MenuMapper extends BaseMapper<Menu> {

    @Select("SELECT  m1.id,  m1.name AS name1,  m1.path AS path1,  m1.icon AS icon1,  m2.name AS name2,  m2.path AS path2,  m2.link_url,  m2.icon AS icon2  \n" +
            "FROM \n" +
            "(SELECT m.* \n" +
            "FROM user u, user_role ur, role r, role_menu rm, menu m  \n" +
            "WHERE u.id = ur.user_id  AND ur.role_id = r.id  AND r.id = rm.role_id  AND rm.menu_id = m.id  AND u.id = #{userId}) m1  \n" +
            "JOIN  \n" +
            "(SELECT m.*  \n" +
            "FROM user u, user_role ur, role r, role_menu rm, menu m  \n" +
            " WHERE u.id = ur.user_id  AND ur.role_id = r.id  AND r.id = rm.role_id  AND rm.menu_id = m.id  AND u.id = #{userId}) m2  \n" +
            "ON m1.id = m2.parentMenuId  \n" +
            "WHERE m1.path = #{path} \n" +
            "ORDER BY m2.priority;")
    List<Map<String, String>> findMap(@Param("userId") Integer userId,@Param("path") String path);

    @Select("select m.*\n" +
            "from user u , user_role ur , role r , role_menu rm , menu m\n" +
            "where u.id = ur.user_id and ur.role_id = r.id and r.id = rm.role_id and rm.menu_id = m.id\n" +
            "and u.id = #{userId} and m.level=#{level};")
    List<Menu> selectMenuByUserId(Integer userId, int level);
}
